Associate the drawing context to the Cairo context
authorEmmanuele Bassi <ebassi@gnome.org>
Tue, 7 Jun 2016 15:34:50 +0000 (16:34 +0100)
committerEmmanuele Bassi <ebassi@gnome.org>
Thu, 9 Jun 2016 16:45:40 +0000 (17:45 +0100)
Instead of associating the GdkWindow that created the GdkDrawingContext
we can directly bind the Cairo context to the GDK drawing context.

Cairo contexts created via gdk_cairo_create() go back to not having a
GdkWindow associated to them, like they did before we introduced the
gdk_window_begin_draw_frame() API.

https://bugzilla.gnome.org/show_bug.cgi?id=766675

docs/reference/gdk/gdk3-sections.txt
gdk/gdkcairo.h
gdk/gdkdrawingcontext.c
gdk/gdkwindow.c
gtk/gtkwidget.c

index f3e005ab068474f7a9e6d5ea822f08a64e8b93f0..b0fa61f4e4f344f58a558bae38678acefdde1974 100644 (file)
@@ -625,7 +625,7 @@ gdk_window_create_similar_surface
 gdk_window_create_similar_image_surface
 gdk_cairo_create
 gdk_cairo_get_clip_rectangle
-gdk_cairo_get_window
+gdk_cairo_get_drawing_context
 gdk_cairo_set_source_color
 gdk_cairo_set_source_rgba
 gdk_cairo_set_source_pixbuf
index 4d8d2235ff25754ed1142378584d267631c44848..6cd50aacac7d2d344435b1316e6d60cf4d0df53d 100644 (file)
@@ -25,6 +25,7 @@
 #include <gdk/gdkversionmacros.h>
 #include <gdk/deprecated/gdkcolor.h>
 #include <gdk/gdkrgba.h>
+#include <gdk/gdkdrawingcontext.h>
 #include <gdk/gdkpixbuf.h>
 #include <pango/pangocairo.h>
 
@@ -32,8 +33,7 @@ G_BEGIN_DECLS
 
 GDK_AVAILABLE_IN_ALL
 cairo_t  * gdk_cairo_create             (GdkWindow          *window);
-GDK_AVAILABLE_IN_3_22
-GdkWindow * gdk_cairo_get_window        (cairo_t            *cr);
+
 GDK_AVAILABLE_IN_ALL
 gboolean   gdk_cairo_get_clip_rectangle (cairo_t            *cr,
                                          GdkRectangle       *rect);
@@ -83,6 +83,8 @@ void       gdk_cairo_draw_from_gl (cairo_t              *cr,
                                    int                   width,
                                    int                   height);
 
+GDK_AVAILABLE_IN_3_22
+GdkDrawingContext *     gdk_cairo_get_drawing_context   (cairo_t *cr);
 
 G_END_DECLS
 
index caffa0cf4840eddf0ce52e5cf92ed8912ec235d9..6216bab20ee51f39af6315870f470f8b1e31a222 100644 (file)
@@ -178,6 +178,34 @@ gdk_drawing_context_init (GdkDrawingContext *self)
 {
 }
 
+static const cairo_user_data_key_t draw_context_key;
+
+static void
+gdk_cairo_set_drawing_context (cairo_t           *cr,
+                               GdkDrawingContext *context)
+{
+  cairo_set_user_data (cr, &draw_context_key, context, NULL);
+}
+
+/**
+ * gdk_cairo_get_drawing_context:
+ * @cr: a Cairo context
+ *
+ * Retrieves the #GdkDrawingContext that created the Cairo
+ * context @cr.
+ *
+ * Returns: (transfer none) (nullable): a #GdkDrawingContext, if any is set
+ *
+ * Since: 3.22
+ */
+GdkDrawingContext *
+gdk_cairo_get_drawing_context (cairo_t *cr)
+{
+  g_return_val_if_fail (cr != NULL, NULL);
+
+  return cairo_get_user_data (cr, &draw_context_key);
+}
+
 /**
  * gdk_drawing_context_get_cairo_context:
  * @context:
@@ -204,6 +232,9 @@ gdk_drawing_context_get_cairo_context (GdkDrawingContext *context)
   if (context->cr == NULL)
     {
       context->cr = gdk_cairo_create (context->window);
+
+      gdk_cairo_set_drawing_context (context->cr, context);
+
       gdk_cairo_region (context->cr, context->clip);
       cairo_clip (context->cr);
     }
index 4724f3fa9f5086a795c48ed14b7c6d94218fbda2..0f3e222811680b3615c640ce9a93ddfe0f5b78ec 100644 (file)
@@ -3111,33 +3111,6 @@ gdk_window_begin_paint_region (GdkWindow            *window,
   gdk_window_begin_paint_internal (window, region);
 }
 
-static const cairo_user_data_key_t draw_context_window_key;
-
-static void
-gdk_cairo_set_window (cairo_t *cr,
-                      GdkWindow *window)
-{
-  cairo_set_user_data (cr, &draw_context_window_key, window, NULL);
-}
-
-/**
- * gdk_cairo_get_window:
- * @cr: a Cairo context created by gdk_window_begin_draw_frame()
- *
- * Retrieves the #GdkWindow that created the Cairo context @cr.
- *
- * Returns: (nullable) (transfer none): a #GdkWindow
- *
- * Since: 3.22
- */
-GdkWindow *
-gdk_cairo_get_window (cairo_t *cr)
-{
-  g_return_val_if_fail (cr != NULL, NULL);
-
-  return cairo_get_user_data (cr, &draw_context_window_key);
-}
-
 /**
  * gdk_window_begin_draw_frame:
  * @window: a #GdkWindow
@@ -3541,8 +3514,6 @@ gdk_cairo_create (GdkWindow *window)
 
   cr = cairo_create (surface);
 
-  gdk_cairo_set_window (cr, window);
-
   if (window->impl_window->current_paint.region != NULL)
     {
       region = cairo_region_copy (window->impl_window->current_paint.region);
index 158e6568525287f8b34f6cf461c6d01d05694514..313a425c88cf634494b8257acb9543b6922f9323 100644 (file)
@@ -6925,6 +6925,7 @@ gboolean
 gtk_cairo_should_draw_window (cairo_t   *cr,
                               GdkWindow *window)
 {
+  GdkDrawingContext *context;
   GdkWindow *tmp;
 
   g_return_val_if_fail (cr != NULL, FALSE);
@@ -6933,8 +6934,11 @@ gtk_cairo_should_draw_window (cairo_t   *cr,
   if (gtk_cairo_is_marked_for_draw (cr))
     return TRUE;
 
-  tmp = gdk_cairo_get_window (cr);
+  context = gdk_cairo_get_drawing_context (cr);
+  if (context == NULL)
+    return TRUE;
 
+  tmp = gdk_drawing_context_get_window (context);
   if (tmp == NULL)
     return TRUE;
 
@@ -6964,22 +6968,25 @@ gtk_widget_draw_internal (GtkWidget *widget,
 
   if (gdk_cairo_get_clip_rectangle (cr, NULL))
     {
-      GdkWindow *event_window;
+      GdkWindow *event_window = NULL;
       gboolean result;
       gboolean push_group;
 
       /* If this was a cairo_t passed via gtk_widget_draw() then we don't
-       * require a window
+       * require a window; otherwise we check for the window associated
+       * to the drawing context and mark it using the clip region of the
+       * Cairo context.
        */
-      if (gtk_cairo_is_marked_for_draw (cr))
-        {
-          event_window = NULL;
-        }
-      else
+      if (!gtk_cairo_is_marked_for_draw (cr))
         {
-          event_window = gdk_cairo_get_window (cr);
-          if (event_window != NULL)
-            gdk_window_mark_paint_from_clip (event_window, cr);
+          GdkDrawingContext *context = gdk_cairo_get_drawing_context (cr);
+
+          if (context != NULL)
+            {
+              event_window = gdk_drawing_context_get_window (context);
+              if (event_window != NULL)
+                gdk_window_mark_paint_from_clip (event_window, cr);
+            }
         }
 
       push_group =